/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.ConnectionLocator;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RotatableDecoration;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.swt.graphics.Color;

import edu.pku.sei.modeler.edit.parts.AbstractLinkEditPart;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.BasicActivities.ControlFlow;

/**
 * @generated
 *
 */
public class ControlFlowEditpart extends UMLLinkEditpart {
	private RotatableDecoration targetDecor;
	private Label guardLabel;
	private Label weightLabel;
	private Label nameLabel;
	
	public ControlFlowEditpart(MetaShapeElement model) {
		super(model);
	}
	
	protected void createEditPolicies() {
		super.createEditPolicies();
	}
	private PolylineConnection getConnection() {
		return (PolylineConnection)getFigure();
	}
	
	
	protected IFigure createFigure() {
		PolylineConnection conn = new PolylineConnection();	
		conn.setLineStyle(1);
		
		guardLabel = new Label(((ControlFlow)this.getLink().getModelElement()).getGuardString());
		weightLabel = new Label(((ControlFlow)this.getLink().getModelElement()).getWeightString());
		nameLabel = new Label(((ControlFlow)this.getLink().getModelElement()).getName());
		
		ConnectionEndpointLocator guardEndpointLocator = new ConnectionEndpointLocator(conn, false);
		guardEndpointLocator.setVDistance(-10);
		conn.add(guardLabel, guardEndpointLocator);
		
		ConnectionEndpointLocator weightEndpointLocator =  new ConnectionEndpointLocator(conn,false);
		weightEndpointLocator.setVDistance(10);
		conn.add(weightLabel,weightEndpointLocator);

		ConnectionLocator nameLocator = new ConnectionLocator(conn);
		nameLocator.setRelativePosition(PositionConstants.NORTH);
		conn.add(nameLabel,nameLocator);
		
		return conn;
	}
	
	private PolylineDecoration createDecoration () {
		PolylineDecoration decoration = new PolylineDecoration();
		decoration.setScale(10, 5);
		return decoration;
	}
	
	
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		refreshVisuals();
	}
	
	protected void refreshVisuals() {
		super.refreshVisuals();
		updateTargetDecoration();
		
		//update guard&weight&name
		PolylineConnection conn = (PolylineConnection)this.getFigure();	
		conn.setLineStyle(1);
		conn.removeAllPoints();
		
		guardLabel.setText(((ControlFlow)this.getLink().getModelElement()).getGuardString());
		weightLabel.setText(((ControlFlow)this.getLink().getModelElement()).getWeightString());
		nameLabel.setText(((ControlFlow)this.getLink().getModelElement()).getName());
		
		ConnectionEndpointLocator guardEndpointLocator = new ConnectionEndpointLocator(conn, false);
		guardEndpointLocator.setVDistance(-10);
		conn.add(guardLabel, guardEndpointLocator);
		
		ConnectionEndpointLocator weightEndpointLocator =  new ConnectionEndpointLocator(conn,false);
		weightEndpointLocator.setVDistance(10);
		conn.add(weightLabel,weightEndpointLocator);

		ConnectionLocator nameLocator = new ConnectionLocator(conn);
		nameLocator.setRelativePosition(PositionConstants.NORTH);
		conn.add(nameLabel,nameLocator);
		
	}
	
	private void updateTargetDecoration() {
		if (targetDecor == null) {
			targetDecor = createDecoration();
			getConnection().setTargetDecoration(targetDecor);
		}
	}
}
